import request, { extend } from 'umi-request';
import { message } from 'antd';

const errorHandler = function(error) {
  if (error.response) {
    if (error.response.status > 400) {
      message.error(error.data.message ? error.data.message : error.data);
      console.log(error.response);
    }
  }
  throw error;
};

const extendRequest = extend({ errorHandler });

export const getRemoteList = async () => {
  return extendRequest('https://public-api-v1.aspirantzhang.com/users', {
    method: 'get',
  })
    .then(function(response) {
      return response;
    })
    .catch(function(error) {
      console.log('getRemoteList catch');
      return false;
    });
};
export const editRecord = async (id, values) => {
  return extendRequest(`https://public-api-v1.aspirantzhang.com/users/${id}`, {
    method: 'put',
    data: values,
  })
    .then(function(response) {
      return true;
    })
    .catch(function(error) {
      console.log('update catch');
      return false;
    });
};
export const addRecord = async (values) => {
  return extendRequest('https://public-api-v1.aspirantzhang.com/users', {
    method: 'post',
    data: values,
  })
    .then(function(response) {
      return true;
    })
    .catch(function(error) {
      console.log('add catch');
      return false;
    });
};
export const deleteRecord = async (id) => {
  return extendRequest(`https://public-api-v1.aspirantzhang.com/users/${id}`, {
    method: 'delete',
  })
    .then(function(response) {
      return true;
    })
    .catch(function(error) {
      console.log('delete catch');
      return false;
    });

};

